﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

using SharpSDL;

namespace SharpSDLMain
{
    class Program
    {
        unsafe static void Main(string[] args)
        {
            SharpSDL.SDL.SDLInitFlags flag = SDL.SDLInitFlags.EVERYTHING;
            SDLInitReturn init = SharpSDL.SDL.Init(flag);
            if (init == SDLInitReturn.Success)
            {
                Console.WriteLine("SDL Library Initialized");
            }
            else
            {
                string Error = new String(SharpSDL.SDL.Error.GetLastError());
                Console.WriteLine("Error in SDL: " + Error);
                return;
            }
            Console.WriteLine(" ");
            Console.WriteLine("Simulating an out of memory error: ");
            SharpSDL.SDL.Error.SetError(SDL.Error.ErrorCode.ENOMEM); //nah, just kidding.....
            string Errore = new String(SharpSDL.SDL.Error.GetLastError());
            Console.WriteLine("Error in SDL: " + Errore);
            Console.WriteLine(" ");
            Console.WriteLine("I' ll Check my current CPU: ");
            SharpSDL.ManagedSDL.CPU CurrentCPU = new SharpSDL.ManagedSDL.CPU();
            if (CurrentCPU.MMXSupport() == SharpSDL.ManagedSDL.CPU.CPUFeature.Present)
                Console.WriteLine("MMX is Supported by the current CPU");
            else
                Console.WriteLine("MMX isn' t Supported by the current CPU");
            if (CurrentCPU.MMXExtSupport() == SharpSDL.ManagedSDL.CPU.CPUFeature.Present)
                Console.WriteLine("MMX Extended is Supported by the current CPU");
            else
                Console.WriteLine("MMX Extended isn' t Supported by the current CPU");
            if (CurrentCPU.Now3DSupport() == SharpSDL.ManagedSDL.CPU.CPUFeature.Present)
                Console.WriteLine("3DNow! is Supported by the current CPU");
            else
                Console.WriteLine("3DNow! isn' t Supported by the current CPU");
            if (CurrentCPU.Now3DExtSupport() == SharpSDL.ManagedSDL.CPU.CPUFeature.Present)
                Console.WriteLine("Extended 3DNow! is Supported by the current CPU");
            else
                Console.WriteLine("Extended 3DNow! isn' t Supported by the current CPU");
            if (CurrentCPU.RDTSCSupport() == SharpSDL.ManagedSDL.CPU.CPUFeature.Present)
                Console.WriteLine("RDTSC is Supported by the current CPU");
            else
                Console.WriteLine("RDTSC isn' t Supported by the current CPU");
            if (CurrentCPU.SSESupport() == SharpSDL.ManagedSDL.CPU.CPUFeature.Present)
                Console.WriteLine("SSE is Supported by the current CPU");
            else
                Console.WriteLine("SSE isn' t Supported by the current CPU");
            if (CurrentCPU.SSE2Support() == SharpSDL.ManagedSDL.CPU.CPUFeature.Present)
                Console.WriteLine("SSE2 is Supported by the current CPU");
            else
                Console.WriteLine("SSE2 isn' t Supported by the current CPU");
            if (CurrentCPU.AltiVecSupport() == SharpSDL.ManagedSDL.CPU.CPUFeature.Present)
                Console.WriteLine("AltiVec is Supported by the current CPU");
            else
                Console.WriteLine("AltiVec isn' t Supported by the current CPU");

            Console.WriteLine(" ");
            int NumOfJoystick = SharpSDL.ManagedSDL.Joystick.JoystickNum();
            Console.WriteLine("Number of plugged Joystick: " + NumOfJoystick.ToString());
            for (int j = 0; j < NumOfJoystick; j++)
            {
                SharpSDL.ManagedSDL.Joystick cj = new SharpSDL.ManagedSDL.Joystick(j);
                if (cj.GetJoystickStatus() == SharpSDL.ManagedSDL.Joystick.JoystickStatus.Closed)
                {
                    Console.WriteLine("Error while opening Joystick");
                }
                else
                {
                    Console.WriteLine("Device Name: " + cj.GetJoystickName());
                    Console.WriteLine("Number of Buttons: " + cj.GetButtonsNumber().ToString());
                    Console.WriteLine("Number of Axis: " + cj.GetAxisNumber().ToString());
                    Console.WriteLine("Number of POV Hats: " + cj.GetHatsNumber().ToString());
                    Console.WriteLine("Number of TrackBalls: " + cj.GetTrackBallsNumber());
                    string Another = String.Empty;
                    do
                    {
                        SharpSDL.ManagedSDL.Joystick.UpdateJoystick();
                        for (int g = 0; g < cj.GetHatsNumber(); g++)
                        {
                            switch (cj.GetHatStatus(g))
                            {
                                case SharpSDL.ManagedSDL.Joystick.HatStatus.Centered:
                                    Console.WriteLine("Centered");
                                    break;
                                case SharpSDL.ManagedSDL.Joystick.HatStatus.Down:
                                    Console.WriteLine("Down");
                                    break;
                                case SharpSDL.ManagedSDL.Joystick.HatStatus.Left:
                                    Console.WriteLine("Left");
                                    break;
                                case SharpSDL.ManagedSDL.Joystick.HatStatus.Up:
                                    Console.WriteLine("Up");
                                    break;
                                case SharpSDL.ManagedSDL.Joystick.HatStatus.Right:
                                    Console.WriteLine("Right");
                                    break;
                                case SharpSDL.ManagedSDL.Joystick.HatStatus.RightUp:
                                    Console.WriteLine("Right and Up");
                                    break;
                                case SharpSDL.ManagedSDL.Joystick.HatStatus.RightDown:
                                    Console.WriteLine("Right and Down");
                                    break;
                                case SharpSDL.ManagedSDL.Joystick.HatStatus.LeftUp:
                                    Console.WriteLine("Left and Up");
                                    break;
                                case SharpSDL.ManagedSDL.Joystick.HatStatus.LeftDown:
                                    Console.WriteLine("Left and Down");
                                    break;
                                default: Console.WriteLine((int)cj.GetHatStatus(g));
                                    break;
                            }
                        }
                        for (int c = 0; c < cj.GetAxisNumber(); c++)
                        {
                            Console.WriteLine("Axe with ID " + c.ToString() + " is in position: " + cj.GetAxeStatus(c).ToString());
                        }
                        for (int c = 0; c < cj.GetButtonsNumber(); c++)
                        {
                            Console.WriteLine("Button with ID " + c.ToString() + " have a state of: " + cj.GetButtonStatus(c).ToString());
                        }
                        Console.WriteLine("Write something to stop.");
                        Another = Console.ReadLine();
                    } while (Another == String.Empty);
                    cj.Dispose();
                }
            }
            SharpSDL.SDL.Error.ClearError();

            int NumOfCDROMDrive = SharpSDL.ManagedSDL.CDROMDrive.CDROMDriveNum();
            string lasterror = new String(SharpSDL.SDL.Error.GetLastError());
            if (lasterror == String.Empty)
            {
                Console.WriteLine("Number of CD-ROM Drives: " + NumOfCDROMDrive.ToString());
                for (int i = 0; i < NumOfCDROMDrive; i++)
                {
                    string name = SharpSDL.ManagedSDL.CDROMDrive.CDROMDriveName(i);
                    Console.WriteLine("The path of CD-ROM Drive Number " + (i + 1).ToString() + " is: " + name);
                    SharpSDL.ManagedSDL.CDROMDrive CurrentCDDrive = new SharpSDL.ManagedSDL.CDROMDrive(i);
                    if (CurrentCDDrive.CDInDrive())
                        Console.WriteLine("There is an audio CD-ROM in drive " + (i + 1));
                    else
                        Console.WriteLine("There is' n an audio CD-ROM in drive " + (i + 1));
                    Console.WriteLine("Would you like to eject the CDROMDrive?");
                    string open = Console.ReadLine();
                    if ((open == "yes") || (open == "Yes") || (open == "YES"))
                        CurrentCDDrive.Eject();
                    CurrentCDDrive.Dispose();
                }
            }
            else
            {
                Console.WriteLine("Error with CDROM");
            }
            Console.WriteLine(" ");
            System.Threading.Thread.Sleep(1000);
            Console.WriteLine(SharpSDL.SDL.ApplicationStatus.AppStatus());
            if (SharpSDL.SDL.ApplicationStatus.AppActive())
                Console.WriteLine("Application is active!");
            else
                Console.WriteLine("Application isn' t active...");
            if (SharpSDL.SDL.ApplicationStatus.AppInputFocus())
                Console.WriteLine("Application has input focus");
            else
                Console.WriteLine("Application hasn' t input focus...");
            if (SharpSDL.SDL.ApplicationStatus.AppMouseFocus())
                Console.WriteLine("Application has the mouse focus!");
            else
                Console.WriteLine("Application hasn' t the mouse focus....");
            Console.WriteLine(" ");
            
            Console.WriteLine(" ");
            Console.WriteLine("Ready to quit SDL, press a button to terminate: ");
            Console.ReadKey(true);
            SharpSDL.SDL.Quit();
        }
    }
}
